//
//  WeatherConditionsVC.m
//  NotepadApp
//
//  Created by yobet on 2018/4/16.
//  Copyright © 2018年 HJ. All rights reserved.
//

@interface WeatherConditionsCell : UICollectionViewCell

@property(nonatomic,strong) UILabel * title;
@property(nonatomic,strong) UIImageView * mainImage;
@end

@implementation WeatherConditionsCell

-(UIImageView *)mainImage {
    if (!_mainImage) {
        _mainImage = [[UIImageView alloc]init];
        _mainImage.contentMode = UIViewContentModeScaleToFill;
        _mainImage.image =[UIImage imageNamed:@"Shoppingcart"];
        [self addSubview:_mainImage];
    }
    return _mainImage;
}
-(UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc]init];
        _title.textColor =HJColorRGB(0);
        _title.font = [UIFont systemFontOfSize:13];
        _title.textAlignment = NSTextAlignmentCenter;
        _title.text= @"123";
        [self addSubview:_title];
    }
    return _title;
}


-(void)layoutSubviews {
    [super layoutSubviews];
    self.mainImage.frame = CGRectMake(self.width/2-(self.width/2), 0, self.width, 50);
    self.title.frame = CGRectMake(0, CGRectGetMaxY(self.mainImage.frame), self.width, 15);
}



@end

#import "WeatherConditionsVC.h"
static NSString * const identifier = @"Cell";

@interface WeatherConditionsVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong) UICollectionView * collectionView;
@property(nonatomic,strong) NSMutableArray * icons;
@end

@implementation WeatherConditionsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"天气";
    [self configCollectionView];
}
-(NSMutableArray *)icons {
    if (!_icons) {
        _icons =@[@{@"icon":@"mist ",@"name":@"薄雾"},@{@"icon":@"heavy snowfall",@"name":@"暴雪"},@{@"icon":@"hail ",@"name":@"冰雹"},@{@"icon":@"heavy snow",@"name":@"大雪"},@{@"icon":@"heavy rain",@"name":@"大雨"},@{@"icon":@"cloudy",@"name":@"多云"},@{@"icon":@"windstorm",@"name":@"风暴"},@{@"icon":@"floating dust",@"name":@"浮尘"},@{@"icon":@"gentle breeze",@"name":@"和风"},@{@"icon":@"hurricane",@"name":@"飓风"},@{@"icon":@"Crazy storm",@"name":@"狂风暴"},@{@"icon":@"thundershower",@"name":@"雷阵雨"},@{@"icon":@"cold",@"name":@"冷"},@{@"icon":@"strong gale",@"name":@"烈风"},@{@"icon":@"tornado",@"name":@"龙卷风"},@{@"icon":@"strong breeze",@"name":@"强风"},@{@"icon":@"strong sandstorm",@"name":@"强沙尘暴"},@{@"icon":@"Strong rain shower",@"name":@"强阵雨"},@{@"icon":@"cool breeze ",@"name":@"清风"},@{@"icon":@"clear ",@"name":@"晴"},@{@"icon":@"Sunny or cloudy between",@"name":@"晴间多云"},@{@"icon":@"hot",@"name":@"热"},@{@"icon":@"tropical storm",@"name":@"热带风暴"},@{@"icon":@"sand storm",@"name":@"沙尘暴"},@{@"icon":@"partly cloudy",@"name":@"少云"},@{@"icon":@"breeze",@"name":@"微风"},@{@"icon":@"haze",@"name":@"雾霾"},@{@"icon":@"light rain",@"name":@"小雨"},@{@"icon":@"blowing sand",@"name":@"扬沙"},@{@"icon":@"cloudy",@"name":@"阴"},@{@"icon":@"cloudy1",@"name":@"有风"},@{@"icon":@"sleet ",@"name":@"雨夹雪"},@{@"icon":@"rainy and snowy",@"name":@"雨雪天气"},@{@"icon":@"snow shower",@"name":@"阵雪"},@{@"icon":@"shower",@"name":@"阵雨"},@{@"icon":@"Rain and snow",@"name":@"阵雨加雪"},@{@"icon":@"moderate snow",@"name":@"中雪"},@{@"icon":@"moderate rain",@"name":@"中雨"},@{@"icon":@"scouther",@"name":@"小阵雨"}].mutableCopy;
    }
    return _icons;
}

-(void)configCollectionView {
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((mScreen_W-120)/4, (mScreen_W-120)/4);
    layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, mScreen_W, mScreen_H-(NavBar_H)) collectionViewLayout:layout];
    self.collectionView.showsVerticalScrollIndicator =NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource =self;
    [self.collectionView registerClass:[WeatherConditionsCell class] forCellWithReuseIdentifier:identifier];
    [self.view addSubview:self.collectionView];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.icons.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WeatherConditionsCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.mainImage.image =[UIImage imageNamed:self.icons[indexPath.row][@"icon"]];
    cell.title.text = self.icons[indexPath.row][@"name"];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.block) {
        self.block(self.icons[indexPath.row][@"icon"]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
@end
